<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>Basic Layout - jQuery EasyUI Demo</title>
	<link rel="stylesheet" type="text/css" href="../css/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../css/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="../css/themes/color.css">
	<script type="text/javascript" src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../js/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../js/jquery.serializejson.min.js"></script>

</head>
<body>

    <div class="easyui-layout" data-options="fit:true">
            <div data-options="region:'west',split:true" title="新闻分类" style="width:200px;">
                    <ul id="tt">

                    </ul>
            </div>
            <div data-options="region:'center',title:'增加新闻',iconCls:'icon-ok'">
                    <table id="dg"></table>


                    <div id="tb" style="padding:2px 5px;">
                        <input id="name" label="用户名称:" style="width:100%;">

                        <a href="#" id="searchData" class="easyui-linkbutton" iconCls="icon-search">搜索</a>
                        <a href="#" id="addData" class="easyui-linkbutton" data-options="iconCls:'icon-add'">添加</a>
                        <a href="#" id="removesData" class="easyui-linkbutton" data-options="iconCls:'icon-remove'">批量删除</a>
                    </div>

                    <div id="dlg" class="easyui-dialog" title="增加新闻" data-options="iconCls:'icon-save',closed: true" style="width:400px;height:400px;padding:10px">

                                    <form id="ff" class="easyui-form" method="post" data-options="novalidate:true">
                                            <input  id="addid" name="newsId" style="width:100%" type="hidden">
                                            <input  id="addid" name="_id" style="width:100%"  type="hidden">


                                        <div style="margin-bottom:20px">
                                            <input class="easyui-textbox" name="title" style="width:100%" data-options="label:'新闻标题:',required:true,missingMessage:'请填写标题'">
                                        </div>
                                        <div style="margin-bottom:20px">
                                            <input class="easyui-textbox"  name="content" style="width:100%" data-options="label:'新闻内容:',required:true,missingMessage:'请填写内容',height:66,multiline:true">
                                        </div>
                                        <div style="margin-bottom:20px">
                                            <input class="easyui-textbox" name="author" style="width:100%" data-options="label:'作者:',required:true,missingMessage:'请填写作者'">
                                        </div>
                                        <div style="margin-bottom:20px">
                                            <input class="easyui-datetimebox" name="date" style="width:100%" data-options="label:'发布时间:',required:true,missingMessage:'请选择时间',showSeconds:false" >
                                        </div>
                                        <div style="margin-bottom:20px">
                                            <input class="easyui-textbox" name="number" style="width:100%" data-options="label:'阅读次数:',required:true,missingMessage:'请填写次数'">
                                        </div>
                                        <div style="margin-bottom:20px">
                                            <input class="easyui-textbox" name="key" style="width:100%" data-options="label:'关键字:',required:true,missingMessage:'请填写关键字'">
                                        </div>
                                        <div style="margin-bottom:20px">
                                            <input class="easyui-textbox" name="type" style="width:100%" data-options="label:'类型:',required:true,missingMessage:'请填写类型'">
                                        </div>
                                        <div style="margin-bottom:20px">
                                            <select class="easyui-combobox" name="length" style="width:100%" data-options="label:'长短:',required:true,missingMessage:'请选择长短',editable:false,panelHeight:'auto'"><option value="长文">长文</option><option value="中文">中文</option><option value="短文">短文</option></select>
                                        </div>
                                        <div style="margin-bottom:20px">
                                            <select class="easyui-combobox" name="hot" style="width:100%" data-options="label:'热度:',required:true,missingMessage:'请选择热度',editable:false,panelHeight:'auto'"><option value="1">热门</option><option value="0">冷门</option></select>
                                        </div>
                                        <div style="margin-bottom:20px">
                                            <input class="easyui-textbox" name="remark" style="width:100%" data-options="label:'备注:',required:true,missingMessage:'请填写备注'">
                                        </div>
                                    </form>
                                    <div style="text-align:center;padding:5px 0">
                                        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()" style="width:80px">提交</a>
                                        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()" style="width:80px">取消</a>
                                    </div>
                    </div>
            </div>
        </div>
        <div id="dlg2" class="easyui-dialog" title="新闻详情" data-options="iconCls:'icon-save',closed: true" style="width:400px;height:400px;padding:10px">
       <p id="newstext"></p>
        </div>
        <!-- 评论模块 -->
        <div id="dlg3" class="easyui-dialog" title="新闻详情" data-options="iconCls:'icon-save',closed: true,fit:true" style="width:800px;height:400px;padding:10px">
            <table id="dgx"></table>
                    <div id="tbx" style="padding:2px 5px;">
                        <input id="namex" label="用户名称:" style="width:100%;">
                        <a href="#" id="searchDatax" class="easyui-linkbutton" iconCls="icon-search">搜索</a>
                        <a href="#" id="addDatax" class="easyui-linkbutton" data-options="iconCls:'icon-add'">添加</a>
                        <a href="#" id="removesDatax" class="easyui-linkbutton" data-options="iconCls:'icon-remove'">批量删除</a>
                    </div>

                    <div id="dlgx" class="easyui-dialog" title="增加评论" data-options="iconCls:'icon-save',closed: true" style="width:400px;height:400px;padding:10px">
                        <form id="ffx" class="easyui-form" method="post" data-options="novalidate:true">
                                <input  id="cid" name="bdid" style="width:100%" type="hidden" >
                                <input  id="cty" name="bdtype" style="width:100%" type="hidden" >
                                <input  id="cbsid" name="_id" style="width:100%" type="hidden" >
                            <div style="margin-bottom:20px">
                                <input class="easyui-textbox" name="pluser" style="width:100%" data-options="label:'评论用户:',required:true,missingMessage:'请填写评论用户'">
                            </div>
                            <div style="margin-bottom:20px">
                                    <select class="easyui-combobox" name="pllvl" style="width:100%" data-options="label:'评论等级:',required:true,missingMessage:'请选择等级',editable:false,panelHeight:'auto'"><option value="好评">好评</option><option value="中评">中评</option><option value="差评">差评</option></select>
                            </div>
                            <div style="margin-bottom:20px">
                                <input class="easyui-textbox"  name="plcontent" style="width:100%" data-options="label:'评论内容:',required:true,missingMessage:'请填写内容',height:66,multiline:true">
                            </div>
                            <div style="margin-bottom:20px">
                                <!-- <input class="easyui-textbox" name="date" style="width:100%" data-options="label:'发布时间:',required:true"> -->
                                <input class="easyui-datetimebox" name="pldate" style="width:100%" data-options="label:'评论时间:',required:true,missingMessage:'请选择时间',showSeconds:false" >
                            </div>
                        </form>
                        <div style="text-align:center;padding:5px 0">
                            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitFormx()" style="width:80px">提交</a>
                            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearFormx()" style="width:80px">取消</a>
                        </div>
                    </div>
        </div>

</body>
</html>

<script>
 var newsId = '';
 $('#tt').tree({
        url:`${window.parent.globalURL}cate/list/1`,
        onClick:function(node){
            $('#dg').datagrid({
                queryParams: {
                    newsId: node._id
                }
            });
        }
});

 function submitForm(){
    $('#ff').form('submit',{
        onSubmit:function(){
           if($(this).form('enableValidation').form('validate')){
                // 将当前页面的表单信息进行获取，然后进行ajax请求
                var formData = $("#ff").serializeJSON();
                    // add
                    //delete formData._id;
                    if(formData._id.length > 0){
                    // update
                    $.ajax({
                        url:`${window.parent.globalURL}news/${formData._id}`,
                        type:'put',
                        data: formData
                    }).done(function(res){
                        $('#dlg').dialog('close');
                        $('#dg').datagrid('reload');
                        $('#ff').form('clear');
                    })
                }else{
                    // add
                    delete formData._id;
                    $.ajax({
                        url:`${window.parent.globalURL}news`,
                        type:'post',
                        data: formData
                    }).done(function(res){
                        $('#dlg').dialog('close');
                        $('#dg').datagrid('reload');
                        $('#ff').form('clear');
                    })
                }
           }
           return false; // 阻止默认事件
            // 表单验证
        }
    });

}


//修改函数
function updateData(id){
    $.ajax({
        url:`${window.parent.globalURL}news/${id}`,
        type:'put'
    }).done(function(res){
        $('#ff').form('load',res);
        $('#dlg').dialog('open');
    })
}


function clearForm(){
    $('#ff').form('clear');
}

function renderTime(date) {
        var dateee = new Date(date).toJSON();
        return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
    }
function hot(boolean){
    var str='';
    if(boolean==true){
       str='热门';
    }else{
        str='冷门'
    }
    return str
}

$('#dg').datagrid({
    url:`${window.parent.globalURL}news/list`,
    fit:'true',
    border:false,
    pagination:true,
    toolbar:'#tb',
    onLoadSuccess:function(data){$("a[name='a1']").linkbutton({plain:true})},
    columns:[[
        {field:'ck',checkbox:true},
        {field:'title',title:'新闻标题',width:100},
        {field:'author',title:'作者',width:100},
        {field:'date',title:'发布时间',width:190,formatter: function(value, row, index) {
                    return `<span>${renderTime(row.date)}</span>`;
                }
},
        {field:'number',title:'阅读次数',width:100},
        {field:'key',title:'关键字',width:100},
        {field:'type',title:'类型',width:100},
        {field:'hot',title:'热度',width:100,formatter: function(value, row, index) {
                    return `<span>${hot(row.hot)}</span>`;
                }},
        {field:'length',title:'长短',width:100},
        {field:'remark',title:'备注',width:100},
        {field:'_id',title:'操作', width:200,
            formatter: function(value,row,index){
                return `<a href="javascript:void(0)" name="a1" class="c3"  onclick="updateData('${row._id}')">修改</a> <a href="javascript:void(0)" name="a1" class="c7" onclick="deleteData('${row._id}')">删除</a>
                <a href="javascript:void(0)" name="a1" class="c8"  onclick="showData('${row._id}')">详情</a>
                <a href="javascript:void(0)" name="a1" class="c8"  onclick="showComment('${row._id}')">评论</a>`
			}
		}
    ]]
});

//xiangqing
function showData(id){
    $.ajax({
        url:`${window.parent.globalURL}news/${id}`,
        type:'get'
    }).done(function(res){
      var str =res.content;
        $('#dlg2 #newstext').html(str);
        $('#dlg2').dialog('open');
    })
}
//评论
var idstr ='';
function showComment(id){
    idstr=id;
    $('#dlg3').dialog('open');
    showrright(id);

}

//评论内功能
//---------------------------------
function showrright(id){
    $('#dgx').datagrid({
    url:`${window.parent.globalURL}comment/list`,
    fit:'true',
    border:false,
    pagination:true,
    toolbar:'#tbx',
    queryParams: {
            pltype:1,
            plid:id ,
    },
    onLoadSuccess:function(data){$("a[name='a2']").linkbutton({plain:true})},
    columns:[[
        {field:'ck',checkbox:true},
        {field:'pluser',title:'用户名称',width:100},
        {field:'pllvl',title:'评论等级',width:100},
        {field:'plcontent',title:'评论内容',width:200},
        {field:'pldate',title:'评论时间',width:190,formatter: function(value, row, index) {
            return `<span>${renderTime(row.pldate)}</span>`;
            }
        },
        {field:'_id',title:'操作', width:150,
            formatter: function(value,row,index){
                return `<a href="javascript:void(0)" name="a2" class="c3"  onclick="updateDatax('${row._id}')">修改</a> <a href="javascript:void(0)" name="a2" class="c7" onclick="deleteDatax('${row._id}')">删除</a>`
            }
		}
    ]]
});
}
$("#addDatax").click(function(){
        $('#ffx').form('clear');
        $('#cid').val(idstr);
        $('#cty').val(1);
        $('#dlgx').dialog('open');
        //$('#addid').val(node);

})

$("#removesDatax").click(function(){
    deleteDatas();
})
 function submitFormx(){
    $('#ffx').form('submit',{
        onSubmit:function(){
           if($(this).form('enableValidation').form('validate')){
                // 将当前页面的表单信息进行获取，然后进行ajax请求
                var formData = $("#ffx").serializeJSON();
                    // add
                    //delete formData._id;
                    if(formData._id.length > 0){
                    // update
                    $.ajax({
                        url:`${window.parent.globalURL}comment/${formData._id}`,
                        type:'put',
                        data: formData
                    }).done(function(res){
                        $('#dlgx').dialog('close');
                        $('#dgx').datagrid('reload');
                        $('#ffx').form('clear');
                    })
                }else{
                    // add
                    delete formData._id;
                    $.ajax({
                        url:`${window.parent.globalURL}comment`,
                        type:'post',
                        data: formData
                    }).done(function(res){
                        $('#dlgx').dialog('close');
                        $('#dgx').datagrid('reload');
                        $('#ffx').form('clear');
                    })
                }
           }
           return false; // 阻止默认事件
            // 表单验证
        }
    });

}


//修改函数
function updateDatax(id){
    $('#ffx').form('clear');
    $.ajax({
        url:`${window.parent.globalURL}comment/${id}`,
        type:'put'
    }).done(function(res){
        $('#ffx').form('load',res);
        $('#dlgx').dialog('open');
    })
}


function deleteDatax(id){
    $.messager.confirm('确认对话框', '你确认删除?', function(r){
        if (r){
            $.ajax({
                url:`${window.parent.globalURL}comment/${id}`,
                type:'delete'
            }).done(function(res){
                $("#dgx").datagrid('reload');
            })
        }
    });

}

//删除多个
function deleteDatasx(){
  var rows = $('#dgx').datagrid('getSelections');
  var ids = [];
  for(var i=0;i<rows.length;i++){
    ids.push(rows[i]._id);
  }

  $.messager.confirm('确认对话框', '你确认删除?', function(r){
        if (r){
            $.ajax({
                url:`${window.parent.globalURL}comment/removes`,
                type:'post',
                data:{
                    ids: ids.toString()
                }
            }).done(function(res){
                $("#dgx").datagrid('reload');
            })
        }
    });

}

$("#searchDatax").click(function(){
    $('#dgx').datagrid({
        queryParams: {
            name: $("#namex").val(),
            plid:idstr
        }
    });
})

//-------------------------------------

function deleteData(id){
    $.messager.confirm('确认对话框', '你确认删除?', function(r){
        if (r){

            $.ajax({
                url:`${window.parent.globalURL}news/${id}`,
                type:'delete'
            }).done(function(res){
                $("#dg").datagrid('reload');
            })
        }
    });

}


//删除多个
function deleteDatas(){
  var rows = $('#dg').datagrid('getSelections');
  var ids = [];
  for(var i=0;i<rows.length;i++){
    ids.push(rows[i]._id);
  }

  $.messager.confirm('确认对话框', '你确认删除?', function(r){
        if (r){
            $.ajax({
                url:`${window.parent.globalURL}news/removes`,
                type:'post',
                data:{
                    ids: ids.toString()
                }
            }).done(function(res){
                $("#dg").datagrid('reload');
            })
        }
    });

}

$("#searchData").click(function(){
    $('#dg').datagrid({
        queryParams: {
            name: $("#name").val()
        }
    });
})


//添加新闻-----------------------------------------
$("#addData").click(function(){
    if($('#tt').tree('getSelected')){
        var node = $('#tt').tree('getSelected')._id;
        $('#dlg').dialog('open');
        $('#addid').val(node);
    }else{
        $.messager.alert('警告','请选择新闻类型');
    }



})

$("#removesData").click(function(){
    deleteDatas();
})
</script>